<!-- 表单 》 星期范围 -->

<template>
  <el-form-item v-bind="formItem">
    <!-- 标签 -->
    <template v-if="hasLabelSlot" #label>
      <slot name="label" :label="formItem.label">{{ formItem.label }}</slot>
    </template>

    <el-date-picker v-model="model" v-bind="commonProps" type="week" @change="emitChange" />
  </el-form-item>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import dayjs from 'dayjs'
import mixForm from '../mixForm'
import FormWeek from '../FormWeek'
import FormDateRange from '../FormDateRange'

const name = 'FormWeekRange'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name,

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [mixForm],

  /**
   * 属性注册 (抽取以便查阅)
   */
  props: CORE.WEB[name],

  /**
   * 状态数据 (函数返回对象)
   */
  data() {
    return {
      model: null, // 内部绑定值 (永远是一周的第二天，即显示行7天中的第二天)
    }
  },

  /**
   * 计算属性 (慎用箭头函数)
   */
  computed: FormWeek.computed,

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 是否逻辑不等 (如不等则调用 setModel() ) (可覆盖)
     */
    isDiff() {
      const { obj, prop, bindList, isMulti, model, init, EMPTY, format, string } = this
      const values = isMulti ? [obj[bindList[0]], obj[bindList[1]]] : obj[prop]
      const begin = CORE.isArray(values) ? values[0] : EMPTY
      const end = CORE.isArray(values) ? values[1] : EMPTY

      if (!model) return init || begin !== EMPTY || end !== EMPTY

      const modelBegin = `${CORE.dateStr(model, -1)} 00:00:00`
      const modelEnd = `${CORE.dateStr(model, 5)} 23:59:59`
      if (format) return begin !== modelBegin || end !== modelEnd
      if (string) return +begin !== CORE.timeNum(modelBegin) || +end !== CORE.timeNum(modelEnd)
      return begin !== CORE.timeNum(modelBegin) || end !== CORE.timeNum(modelEnd)
    },

    /**
     * 设置内部绑定值 (可覆盖)
     */
    setModel() {
      const { obj, prop, bindList, isMulti, init, fixValue } = this
      const values = isMulti ? [obj[bindList[0]], obj[bindList[1]]] : obj[prop]
      const initDate = init ? fixValue(init) : null

      if (!CORE.isArray(values)) {
        this.model = initDate
        return
      }

      this.model = fixValue(values[0]) || initDate
    },

    /**
     * 获取最终回传值 (可覆盖)
     */
    getValue() {
      const { model, EMPTY, format, string } = this
      if (!model) return [EMPTY, EMPTY]

      const modelBegin = `${CORE.dateStr(model, -1)} 00:00:00`
      const modelEnd = `${CORE.dateStr(model, 5)} 23:59:59`
      if (format) return [modelBegin, modelEnd]
      if (string) return [`${CORE.timeNum(modelBegin)}`, `${CORE.timeNum(modelEnd)}`]
      return [CORE.timeNum(modelBegin), CORE.timeNum(modelEnd)]
    },

    /**
     * 是否物理相等 (如不等可直接用 value 赋值给 obj[bind] ) (可覆盖)
     */
    equals: FormDateRange.methods.equals,

    /**
     * 提交改变 (可覆盖)
     */
    emitChange: FormDateRange.methods.emitChange,

    /**
     * 修复的数据
     */
    fixValue: FormWeek.methods.fixValue,

    /**
     * 禁用日期
     */
    disabledDate(date) {
      const { history, future } = this
      if (!date) return false
      if (history) return date.getTime() > dayjs().valueOf()
      if (future) return date.getTime() < dayjs().startOf('day').valueOf()
      return false
    },
  },
}
</script>
